package com.atguigu.guli.service.cms.controller.api;

import com.atguigu.guli.service.base.result.R;
import com.atguigu.guli.service.cms.entity.Ad;
import com.atguigu.guli.service.cms.service.AdService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author fenge
 */
//@CrossOrigin
@Api(tags = "广告推荐")
@RestController
@RequestMapping("/api/cms/ad")
public class ApiAdController {

    @Autowired
    private AdService adService;

    @Autowired
    private RedisTemplate redisTemplate;

    @PostMapping("/save-test")
    public R saveAd(@RequestBody Ad ad){
        redisTemplate.opsForValue().set("ad",ad);
        return R.ok();
    }

    @GetMapping("/get-test/{key}")
    public R getAd(@PathVariable String key){
        Ad ad = (Ad)redisTemplate.opsForValue().get(key);
        return R.ok().data("ad",ad);
    }

    @DeleteMapping("/remove-test/{key}")
    public R removeAd(@PathVariable String key){
        Boolean delete = redisTemplate.delete(key);
        System.out.println(delete);
        Boolean hasKey = redisTemplate.hasKey(key);
        System.out.println(hasKey);
        return R.ok();
    }

    @ApiOperation("查询首页广告推荐")
    @GetMapping("/list")
    public R listAds(){
        Map adsMap = adService.listAds();
        return R.ok().data("items",adsMap);
    }
}
